package com.team.ai.service;

import cn.hutool.core.date.DateTime;
import com.team.ai.entity.Community;
import com.team.ai.entity.Concern;
import com.team.ai.entity.Like;
import com.team.ai.entity.ResultEntity;
import com.team.ai.mapper.*;
import com.team.ai.tool.KoDoTool;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
public class CommunityService {
    @Resource
    CommunityMapper communityMapper;
    @Resource
    CommentsMapper commentsMapper;
    @Resource
    ConcernMapper concernMapper;

    @Resource
    UserMapper userMapper;
    @Resource
    LikeMapper likeMapper;

    public String insert(int userId, String content,String pic,String name,String img){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = DateTime.now();
            String dateStr = sdf.format(now);
            Date date = sdf.parse(dateStr);
            System.out.println(now);

//            String uName= String.valueOf(userMapper.selectByName(name));
            communityMapper.insert(userId,content,date,pic,name,img);
            List<Object> list = new ArrayList<>();
            list.add(communityMapper.selectList());
            list.add(commentsMapper.selectList());
            return ResultEntity.success(list) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }

    public String update(int likesize,int id){
        try {
            return ResultEntity.success(communityMapper.update(likesize, id)) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }



    public String selectByUserIda(int userId){
        try{
            List<Object> list = new ArrayList<>();
            List<Community> temp = communityMapper.selectList();
            HashSet<Integer> gz = new HashSet<>();   // 存放被关注者的用户id
            List<Concern> bgz = concernMapper.selectList(userId);  //        list = select 关注表  条件：userID=当前用户

            for (Concern integer : bgz) {
                gz.add(integer.getBefollowedid());
            }
            for (Community o : temp) {
                if (gz.contains(o.getUserid())){
                    o.setIsgz(1);
                }
            }
            HashSet<Integer> like = new HashSet<>(); //被关注文章的id
            List<Like> likes=likeMapper.selectList(userId);
            for (Like like1 : likes) {
                like.add(like1.getLikeid());
            }
            for (Community community : temp) {
                if (like.contains(community.getId())){
                    community.setIslike(1);
                }
            }
            list.add(temp);
            list.add(commentsMapper.selectList());
            return ResultEntity.success(list) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }
    public String selectById(int id,int isgz ,int islike){
        try{
            Community community = communityMapper.selectById(id);
            community.setIslike(islike);
            community.setIsgz(isgz);
            return ResultEntity.success(community) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }

    public String selectByUserId(int userId){
        try{
            return ResultEntity.success(communityMapper.selectByUserId(userId)) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }

}
